import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/account/login.vue'
import Layout from '../components/layout/Layout.vue'
import UserList from '../components/user/UserList'
import Role from '../components/power/role'
import Auth from '../components/power/auth.vue'
import Categories from '../components/goods/categories.vue'
import Params from '../components/goods/params.vue'
Vue.use(VueRouter)

const router = new VueRouter({
    routes:[
        {path:'/',redirect:'/login'},
        {path:'/login',meta:{unRequiresAuth:true},component:Login},
        {path:'/layout',component:Layout,children:[
            {
                path:'users',       //嵌套路由的path不要加斜杠  直接写组件的名称
                component:UserList
            },
            {
                path:'roles',
                component:Role
            },
            {
                path:'rights',
                component:Auth
            },
            {
                path:'categories',
                component:Categories
            },
            {
                path:'params',
                component:Params
            }
        ]}
    ]
})

router.beforeEach((to,from,next)=>{
    if (to.meta.unRequiresAuth){ // 不需要权限验证
        next()
    }else { // 需要权限验证
        // 判断本地是否有token，如果有则代表登录，如果没有跳转到login页面
        if (localStorage.getItem('myToken')){
           next()
       } else { // 没有登录，跳转到登录页面【编程式】
           return router.push({path:'/login'})
       }
    }
})

export default router
